import { siteDetail } from "../../../api/drivingSchool"
import { shareAppMessageSet } from "../../../utils/config"
import { dataCollection } from "../../../utils/dataCollection"
import { swiperProps } from "../../../utils/type"

//场地周边配套
export const siteMating: { value: string, label: string }[] = [
  { value: '1', label: '厕所' },
  { value: '2', label: '休息场地' },
  { value: '3', label: '储物柜' },
  { value: '4', label: '共享单车' },
  { value: '5', label: '餐饮店' },
  { value: '6', label: '商场' },
  { value: '7', label: '便利店' }
]

let key = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mating: [
      { src: '../../../images/kecdxq_icon_zbpt_cs@2x.png', value: '1', name: '厕所', visible: false },
      { src: '../../../images/kecdxq_icon_zbpt_xxcs@2x.png', value: '2', name: '休息场所', visible: false },
      { src: '../../../images/kecdxq_icon_zbpt_cwg@2x.png', value: '3', name: '储物柜', visible: false },
      { src: '../../../images/kecdxq_icon_zbpt_gxdc@2x.png', value: '4', name: '共享单车', visible: false },
      { src: '../../../images/kecdxq_icon_zbpt_cyd@2x.png', value: '5', name: '餐饮店', visible: false },
      { src: '../../../images/kecdxq_icon_zbpt_bld@2x.png', value: '7', name: '便利店', visible: false },],
    swiperList: <swiperProps[]>[],
    detail: <any>{},
    addressDetail: '',
    subway: '',
    subwayDistance: '',
    busDistance: '',
    busLine: '',
    busStation: '',
    loading: true,
    isPC: false,
    pageReady: false,
    themeCode: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    //从本地获取当前选择的城市编码
    this.setData({
      themeCode: wx.getStorageSync('themeCode')
    })

    key = options.name
    siteDetail(options.id).then(res => {
      const arr: swiperProps[] = []
      res.result.siteVideo.forEach((item: any) => {
        const obj: swiperProps = {
          type: 'video',
          url: item.videoUrl,
          poster: item.coverUrl
        }
        arr.push(obj)
      })
      res.result.sitePic.forEach((item: any) => {
        const obj: swiperProps = {
          type: 'image',
          url: item.picUrl
        }
        arr.push(obj)
      })

      const matingList = this.data.mating

      res.result.supportFacility.forEach((item: string) => {
        matingList.forEach(child => {
          if (Number(item) === Number(child.value)) {
            child.visible = true
          }
        })
      })

      this.setData({
        swiperList: [...arr],
        detail: res.result,
        addressDetail: res.result.addressDetail.replace(/.+?(省|市|自治区|自治州|县|区)/g, ''),
        subway: res.result.trainSiteNear.subway.title,
        subwayDistance: res.result.trainSiteNear.subway.distance,
        busDistance: res.result.trainSiteNear.bus.distance,
        busLine: res.result.trainSiteNear.bus.address,
        busStation: res.result.trainSiteNear.bus.title,
        mating: matingList,
        loading: false,
        isPC: wx.getStorageSync('isPC') || false,
        pageReady: true
      })
    })
  },
  //打开地图
  onOpenMap() {
    // const customOpenMapComponent = this.selectComponent('#customOpenMap')
    // customOpenMapComponent.openMap({
    //   latitude: Number(this.data.detail.lat),
    //   longitude: Number(this.data.detail.lng),
    //   destination: this.data.detail.name,
    // })

    wx.openLocation({
      latitude: Number(this.data.detail.lat),
      longitude: Number(this.data.detail.lng),
      scale: 14,
      name: this.data.detail.name
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return { ...shareAppMessageSet() }
  },
  onHide() {
    dataCollection.setPageMapData('leave')
    dataCollection.setComprehensiveMapData(key, 'leave', 'site')
  },
  onShow() {
    dataCollection.setPageMapData('enter', '科二页面')
    dataCollection.setComprehensiveMapData(key, 'enter', 'site')
  },
  onUnload() {
    dataCollection.setPageMapData('leave')
    dataCollection.setComprehensiveMapData(key, 'leave', 'site')
  },
})